package light.head.dao.site;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.FileInfo;
import light.head.model.PolicySupport;
import light.head.model.PolicySupportAnnex;

import org.springframework.stereotype.Service;
/**
 *	政策支持
 */
@Service
public class PolicySupportDAO extends BaseDAO<PolicySupport, Serializable>{
	private static final String sql_indexlist = "SELECT ps.code, ps.title , ps.rt FROM PolicySupport ps  WHERE ps.deleted = 0 AND ps.type = ? ORDER BY ps.topTime DESC LIMIT 6";
	private static final String sql_getDetail = "FROM PolicySupport ps  WHERE ps.deleted = 0 AND ps.code = ? AND ps.type = ?";
	private static final String sql_findAnnex = "FROM PolicySupportAnnex psa WHERE psa.psc = ?";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM PolicySupport ps WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String type=map.get(Parm.TYPE);
			String title = map.get(Parm.TITLE);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			if(type!=""&&isNotNull(type)){
				hql.append(" AND ps.type = ? ");
				list.add(Byte.parseByte(type));
			}
			if (title != "" && isNotNull(title)) {
				hql.append(" AND ps.title LIKE ? ");
				list.add("%" + title + "%");
			}
			if(time1!=""&&isNotNull(time1)){
				hql.append(" AND ps.rt >= ? ");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if(time2!=""&&isNotNull(time2)){
				hql.append(" AND ps.rt <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			if(deleted!=""&&isNotNull(deleted)){
				hql.append(" AND ps.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY ps.topTime DESC");

		return findToPageBean(hql.toString(), pageBean, list);
	}
	//获取首页List
	public List<PolicySupport> getIndexList(Byte type){
		return query(sql_indexlist,null,null,type);
	}
	
	/**
	 * 根据拼接好的文件编号序列获取对应的文件列表
	 */
	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}

	public List<PolicySupportAnnex> findAnnexByCode(Integer code) {
		return super.find(sql_findAnnex, code);
	}

	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			if(o != null){
				sql.append("?,");
				parm.add(o);
			}
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND psa.code NOT IN(").deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM PolicySupportAnnex psa WHERE psa.psc = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	
	public PolicySupport getDetail(Integer code,String type){
		return (PolicySupport)super.get(sql_getDetail, code,Byte.parseByte(type));
	}
}
